﻿using System;
using System.IO;

namespace padmsg
{
    using padmsg._api;
    using SanlieHash.Fast;
    using yunio;
    using yunio.websocket;

    public class PadMsgAPI
    {
        internal string uri { get; set; }
        internal string adminkey { get; set; }
        internal string token { get; set; }
        public PadMsgAPI(string uri, string adminkey)
        {
            this.uri = uri;
            this.adminkey = adminkey;
        }
        public void SetToken(string token)
        {
            this.token = token;
        }
        public void SetAdminKey(string key)
        {
            this.adminkey = key;
        }
        public static PadMsgAPI Link(string uri, string token)
        {
            var api = new PadMsgAPI(uri, string.Empty);
            api.SetToken(token);
            return api;
        }
        public Stream cdn_download_file(string aes_key, string fileuri)
        {
            var json = padapi.download_cdn(this.uri, this.token, aes_key, 5, fileuri);

            if (string.IsNullOrEmpty(json.Text("Data.FileData"))) return default;
            return new MemoryStream(FastEncode.Base64Decode2Bytes(json.Text("Data.FileData")));
        }
        public YunioJson send_file(string to_userid, string filename, string base64)
        {
            var json = padapi.send_file(this.uri, this.token, to_userid, filename, base64);
            return json;
        }
        public YunioJson send_appmsg(string to_userid, string app_message)
        {
            var json = padapi.send_appmsg(this.uri, this.token, to_userid, app_message, 0);
            return json;
        }
        public YunioJson send_voice(string to_userid, string base64, int duration, int format)
        {
            var json = padapi.send_voice(this.uri, this.token, to_userid, base64, duration, format);
            return json;
        }
        public YunioJson send_emoji(string to_userid, string md5, int size)
        {
            var json = padapi.send_emoji(this.uri, this.token, to_userid, md5, size);
            return json;
        }
        public YunioJson send_image(string to_userid, string imageuri)
        {
            var json = padapi.send_image(this.uri, this.token, to_userid, imageuri);
            return json;
        }
        public YunioJson send_text(string to_userid, string content)
        {
            var json = padapi.send_text(this.uri, this.token, to_userid, content);
            return json;
        }
        public YunioJson get_loginstatus()
        {
            var json = padapi.get_loginstatus(this.uri, this.token);
            return json;
        }
        public bool IsLogin()
        {
            if (get_loginstatus().Number("Code") == 200)
                return true;
            return false;
        }
        public YunioJson wakeup_login()
        {
            var json = padapi.wakeup_login(this.uri, this.token);
            return json;
        }
        public YunioJson download_login_QRcode()
        {
            var json = padapi.download_login_QRcode(this.uri, this.token);
            return json;
        }
        public byte[] DownloadLoginQRcode()
        {
            var json = padapi.download_login_QRcode(this.uri, this.token);
            if (json.Number("Code") == 200)
            {
                var qr_base64 = json.Text("Data.qrCodeBase64");
                return FastEncode.Base64Decode2Bytes(qr_base64.Substring(qr_base64.LastIndexOf(",") +1));
            }
            return new byte[0];
        }
        public static IYunioWsClient ListenMsg(string uri, string token, YunioOnMessage on_message)
        {
            uri += "/ws/GetSyncMsg";

            return YunioWebSocket.Listen(uri, YunioParam.New("key", token), on_message);
        }
        public static void on_message(YunioData data)
        {
            if (data.DataType == YunioDataType.Text) Console.WriteLine(data.Text);
            if (data.DataType == YunioDataType.Json)
            {
                if (data.Json.Text("type") == "heartbeat") return;
                if (data.Json.Text("type") == "welcome") Console.WriteLine(data.Json.Text("message"));
                else
                {
                    Console.WriteLine("----");
                    Console.WriteLine($"msg id: {data.Json.Text("msg_id")}");
                    Console.WriteLine($"from: {data.Json.Text("from_user_name.str")}");
                    Console.WriteLine(data.Json.Text("content.str"));
                    return;
                }
            }
        }
    }
}
